package com.yanqu.road.server.agent.inter;

import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.entity.enums.eAgentErrorCode;
import com.yanqu.road.logic.bussiness.player.UserBussiness;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.server.agent.AgentInterfaceMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.ForbiddenMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;

/**
 * 封禁
 */
public class AgentForbid {

	/**
	 * 对玩家进行封号
	 */
	public String forbidPlayer(JSONObject param) {
		JSONObject ret = AgentInterfaceMgr.getDefaultRet();
		long blackForbiddenTime = param.getLong("blackForbiddenTime");
		String[] userIdList = param.getString("userID").split(",");
		doForbidPlayer(blackForbiddenTime, userIdList);
		AgentInterfaceMgr.setCommonRet(ret, eAgentErrorCode.Success.getValue());

		return ret.toString();
	}

	/**
	 * 对玩家禁言
	 */
	public String forbidPlayerSpeak(JSONObject param) {
		JSONObject ret = AgentInterfaceMgr.getDefaultRet();
		long forbiddenSpeakTime = param.getLong("forbiddenSpeakTime");
		String[] userIdList = param.getString("userID").split(",");
		doForbidSpeak(forbiddenSpeakTime, userIdList);
		AgentInterfaceMgr.setCommonRet(ret, eAgentErrorCode.Success.getValue());

		return ret.toString();
	}

	/**
	 * 对玩家禁言 一定时间
	 */
	public String forbidPlayerSpeakByTime(JSONObject param) {
		JSONObject ret = AgentInterfaceMgr.getDefaultRet();
		int time = param.getIntValue("time");
		String[] userIdList = param.getString("userID").split(",");
		doForbidSpeak(System.currentTimeMillis() + time * 60 * 1000, userIdList);
		AgentInterfaceMgr.setCommonRet(ret, eAgentErrorCode.Success.getValue());

		return ret.toString();
	}

	/**
	 * 对ip进行封号
	 */
	public String forbidPlayerIp(JSONObject param) {
		JSONObject ret = AgentInterfaceMgr.getDefaultRet();
		boolean forbidden = param.getBoolean("forbidden");
		String[] ipList = param.getString("ipList").split(",");
		doForbidByIp(forbidden, ipList);
		AgentInterfaceMgr.setCommonRet(ret, eAgentErrorCode.Success.getValue());

		return ret.toString();
	}

	private void doForbidByIp(boolean isForbid, String[] ipList) {
		if (isForbid) {
			ForbiddenMgr.addForbiddenIp(ipList, null);
		} else {
			ForbiddenMgr.removeForbiddenIp(ipList, null);
		}
	}

	private static void doForbidPlayer(long blackForbiddenTime, String[] playerIds) {
		for (int i = 0; i < playerIds.length; i++) {
			long id = Long.parseLong(playerIds[i]);
			GamePlayer player = GamePlayerMgr.getOnlinePlayer(id);
			if (player != null) {
				UserInfo uInfo = player.getUserInfo();
				uInfo.setBlackListEndTime(blackForbiddenTime);
			} else {
				UserBussiness.updateForbid(id, blackForbiddenTime);
			}
			UserMgr.setBlackListEndTime(id, blackForbiddenTime);
			//踢下线
			AgentPlayer.executeKillPlayer(id);
		}
	}

	private static void doForbidSpeak(long forbiddenSpeakTime, String[] playerIds) {
		for (int i = 0; i < playerIds.length; i++) {
			long id = Long.parseLong(playerIds[i]);
			GamePlayer player = GamePlayerMgr.getOnlinePlayer(id);
			if (player != null) {
				UserInfo uInfo = player.getUserInfo();
				uInfo.setForbiddenSpeakEndTime(forbiddenSpeakTime);
			} else {
				UserBussiness.updateForbidSpeak(id, forbiddenSpeakTime);
			}
		}
	}

}
